// Copyright (C) 2009 Jesse Jones
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using MmmDoc;
using System;
using System.Linq;

namespace MmmDoc.Html
{
	/// <summary>Generates an index pointing to each namespaces table.</summary>
	internal sealed class GenerateNamespacesToc : WriteNormalHtml
	{
		public GenerateNamespacesToc(string[] namespaces) : base("namespaces-toc.html", "namespaces-toc.css")
		{
			m_namespaces = namespaces;
		}
		
		#region Protected Methods
		protected override string OnTitle
		{
			get {return "Namespaces";}
		}
		
		protected override void OnWriteBodyContents(Writer writer)
		{
			writer.WriteStartTagLine("TABLE");
				writer.WriteStartTagLine("COLGROUP");
					writer.WriteElementLine("COL", null);
				writer.WriteEndTagLine("COLGROUP");
				
				writer.WriteStartTagLine("TBODY");
					foreach (string ns in m_namespaces)
					{
						writer.WriteStartTagLine("TR", "class", "link");
							writer.WriteStartTag("TD");
								if (string.IsNullOrEmpty(ns))
									writer.WriteElement("A", "Global", "href", "namespaces-file.html#Globals", "target", "contents", "class", "link");
								else
									writer.WriteElement("A", ns, "href", "namespaces-file.html#" + ns, "target", "contents", "class", "link");
							writer.WriteEndTag("TD");
							writer.WriteLine();
						writer.WriteEndTagLine("TR");
					}
					
					if (Settings.SeeAlsoLinks.Any())
					{
						DoWriteBlank(writer);
						
						writer.WriteStartTagLine("TR", "class", "section");
							writer.WriteElementLine("TD", "See Also", "class", "section");
						writer.WriteEndTagLine("TR");
						
						foreach (string link in Settings.SeeAlsoLinks)
						{
							int i = link.IndexOf(' ');
							string url = link.Substring(0, i);
							string name = link.Substring(i + 1);
							
							writer.WriteStartTagLine("TR", "class", "link");
								writer.WriteStartTag("TD");
									writer.WriteElement("A", name, "href", url, "target", "_top", "class", "link");
								writer.WriteEndTag("TD");
								writer.WriteLine();
							writer.WriteEndTagLine("TR");
						}
					}
				writer.WriteEndTagLine("TBODY");
			writer.WriteEndTagLine("TABLE");
		}
		
		private const string NonBreakingWidthSpace = "\xA0";

		private void DoWriteBlank(Writer writer)
		{
			writer.WriteStartTagLine("TR");
				writer.WriteElement("TD", NonBreakingWidthSpace, "colspan", "2", "class", "separator");
				writer.WriteLine();
			writer.WriteEndTagLine("TR");
		}
		#endregion
		
		#region Fields
		private string[] m_namespaces;
		#endregion
	}
}
